package com.hog.listener;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

/**
 * 监听程序启动完成
 *
 * @author 糖先森
 * @date 2018年03月21日
 * @since JDK 1.8
 */
@Component
public class ApplicationStartupListener implements ApplicationListener<ContextRefreshedEvent> {
    private final static Logger LOG = LoggerFactory.getLogger(ApplicationStartupListener.class);
    @Value("${server.port}")
    private int serverPort = 0;
    
    @Value("classpath:logo.txt")
    private Resource txt;
    @Value("${hog.product.name}")
    private String name;
    @Value("${hog.product.time}")
    private String time;
    
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    	try(BufferedReader br = new BufferedReader(new InputStreamReader(txt.getInputStream(), "utf-8"));) {
			String str=null;
			while((str=br.readLine())!=null) {
				str=str.replace("${hog.product.name}", name);
				str=str.replace("${hog.product.time}", time);
				System.out.println(new String(str));
			}
		} catch (IOException e) {
			LOG.error("loge文件显示失败",e);
		} ;
    	LOG.info("程序启动完成, 访问: http://localhost:{}/", serverPort);
    }
}